package cn.itsource.pet.controller;

import cn.itsource.common.domain.LoginData;
import cn.itsource.common.util.JwtUtils;
import cn.itsource.common.util.Payload;
import cn.itsource.config.LoginInterceptor;
import cn.itsource.pet.ro.PublishRo;
import cn.itsource.pet.service.ISearchMasterMsgService;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.query.SearchMasterMsgQuery;
import cn.itsource.common.util.AjaxResult;
import cn.itsource.common.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    @GetMapping("/getUserPublishMsg")
    public AjaxResult getUserPublishMsg(HttpServletRequest request){
        Payload<LoginData> infoFromToken =
                JwtUtils.getInfoFromToken(request.getHeader(LoginInterceptor.TOKEN), LoginData.class);
        LoginData loginData = infoFromToken.getLoginData();
        return searchMasterMsgService.getUserPublishMsg(loginData.getLogininfo().getId());
    }

    /**
     * 拒单  只删除shopid
     * @param msgId
     * @return
     */
    @GetMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable("id")Long msgId){
        return searchMasterMsgService.reject(msgId);
    }

    /**
     * 拒单  只删除shopid
     * @param msgId
     * @return
     */
    @GetMapping("/accept/{id}")
    public AjaxResult accept(@PathVariable("id")Long msgId){
        return searchMasterMsgService.accept(msgId);
    }

    @PostMapping("/getPool")
    public PageList<SearchMasterMsg> getPool(@RequestBody SearchMasterMsgQuery query){
        return searchMasterMsgService.getPool(query);
    }

    @PostMapping("/getOk")
    public PageList<SearchMasterMsg> getOk(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        Payload<LoginData> infoFromToken =
                JwtUtils.getInfoFromToken(request.getHeader(LoginInterceptor.TOKEN), LoginData.class);
        LoginData loginData = infoFromToken.getLoginData();
        return searchMasterMsgService.getOk(query,loginData.getLogininfo().getId());
    }



    @PostMapping("/getHandle")
    public PageList<SearchMasterMsg> getHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        Payload<LoginData> infoFromToken =
                JwtUtils.getInfoFromToken(request.getHeader(LoginInterceptor.TOKEN), LoginData.class);
        LoginData loginData = infoFromToken.getLoginData();
        return searchMasterMsgService.getHandle(query,loginData.getLogininfo().getId());
    }

    @GetMapping("/taking/{id}")
    public AjaxResult taking(@PathVariable("id") Long id, HttpServletRequest request){
        Payload<LoginData> infoFromToken =
                JwtUtils.getInfoFromToken(request.getHeader(LoginInterceptor.TOKEN), LoginData.class);
        LoginData loginData = infoFromToken.getLoginData();
        return searchMasterMsgService.taking(id,loginData.getLogininfo().getId());
    }

    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody PublishRo publishRo, HttpServletRequest request){
        Payload<LoginData> infoFromToken =
                JwtUtils.getInfoFromToken(request.getHeader(LoginInterceptor.TOKEN), LoginData.class);
        LoginData loginData = infoFromToken.getLoginData();
        Long id = loginData.getLogininfo().getId();//这个东西是不是绝对有
        return searchMasterMsgService.publish(publishRo,id);
    }

    /**
     * 接口：添加或修改
     * @param searchMasterMsg  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){ 
        if( searchMasterMsg.getId()!=null){
		searchMasterMsgService.update(searchMasterMsg);
        }else{
                searchMasterMsgService.save(searchMasterMsg);
	}
        return new AjaxResult();
       
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        searchMasterMsgService.deleteById(id);
        return new AjaxResult();
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        searchMasterMsgService.batchDelete(ids);
        return new AjaxResult();
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public SearchMasterMsg findOne(@PathVariable("id")Long id) {
        return searchMasterMsgService.findById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<SearchMasterMsg> findAll(){
        return searchMasterMsgService.findAll();
    }


   /**
    * 责任分离
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.findPageList(query);
    }
}
